package zero.tech.games.log.hot;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import zero.tech.core.data.PageBean;
import zero.tech.games.common.RedisConsts;
import zero.tech.games.entity.log.LogWaterRewardGet;
import zero.tech.games.log.mapper.LogWaterRewardGetMapper;
import zero.tech.redis.cache.RedisUtil;

@Component
public class LogWaterRewardGetUtil {
	@Autowired
	private RedisUtil redisUtil;
	@Autowired
	private LogWaterRewardGetMapper logWaterRewardGetMapper;
	public void insertBatch(List<LogWaterRewardGet> logWaterRewardGets) {
		logWaterRewardGetMapper.insertBatch(logWaterRewardGets);
		List<String> list=new ArrayList<String>();
		for(LogWaterRewardGet logWaterRewardGet:logWaterRewardGets) {
			int club_id=logWaterRewardGet.getClub_id();
			int uid=logWaterRewardGet.getUid();
			String str=club_id+"_"+uid;
			if(!list.contains(str)) {
				list.add(str);
			}
		}
		for(String str:list) {
			redisUtil.del(RedisConsts.HOT_LOG_WATER_REWARD_GET_PAGE+"_"+str);
		}
	}
	@SuppressWarnings("unchecked")
	public PageBean<LogWaterRewardGet> listWaterRewardGetLogs(int uid, int club_id, int page, int pageLength,
			Date begin, Date end) {
		PageBean<LogWaterRewardGet> pageBean=(PageBean<LogWaterRewardGet>) redisUtil.hget(RedisConsts.HOT_LOG_WATER_REWARD_GET_PAGE+"_"+club_id+"_"+uid,page+"_"+pageLength+"_"+begin.getTime()+"_"+end.getTime());
		if(pageBean==null) {
			int totalCount=logWaterRewardGetMapper.totalCount(club_id,uid,begin,end);
			int start=page*pageLength;
			List<LogWaterRewardGet> logWaterRewardGets=logWaterRewardGetMapper.listWaterRewardGetByPage(club_id,uid,start,pageLength,begin,end);
			pageBean = new PageBean<>(totalCount, pageLength, page, logWaterRewardGets);
			redisUtil.hset(RedisConsts.HOT_LOG_WATER_REWARD_GET_PAGE+"_"+club_id+"_"+uid,page+"_"+pageLength+"_"+begin.getTime()+"_"+end.getTime(), pageBean,5*60);
		}
		return pageBean;
	}

}
